package com.thams.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.thams.entity.InventoryClass;
import com.thams.service.i.IU8Service;
import com.thams.util.MessagePrinter;
import com.thams.util.ParseRequestUtils;

public class InventoryClassServlet extends HttpServlet {

	private static IU8Service service;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String forward = request.getParameter("forward");
		if (null == forward || "".equals(forward)) {
			MessagePrinter.printErrorMessage("请检查访问参数是否正确",request, response);
			return;
		}
		service = (IU8Service) WebApplicationContextUtils
				.getRequiredWebApplicationContext(
						request.getSession().getServletContext()).getBean(
						"u8Service");
		if (forward.equals("listMootoolsTreeData")) {
			MessagePrinter.printObject(service.getInventoryClassListJson(),request, response);
			return;
		}
		if (forward.equals("listJqueryTreeData")) {
			MessagePrinter.printObject(service.getInventoryClassListJsonForJquery(),request, response);
			return;
		}
		if (forward.equals("add")) {
			addInventoryClass(request, response);
		}
	}

	private void addInventoryClass(HttpServletRequest request,
			HttpServletResponse response) {
		InventoryClass inv = new InventoryClass();
		inv = (InventoryClass) ParseRequestUtils.parseRequest(request, inv);
		try {
			service.insertInventoryClass(inv);
			MessagePrinter.printSuccessMessage("添加分类成功",request, response);
		} catch (Exception e) {
			e.printStackTrace();
			MessagePrinter.printErrorMessage("请检查数据是否重复,或者字段不对应!",request,
					response);
		}
	}

	

}
